package com.example.thymeleafdemo.controller;

import com.example.thymeleafdemo.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ClassName DemoController
 * @Description
 * @Author zhangsw
 * @Date 2020/10/15 16:50
 * @Version 1.0
 */
@Controller
public class DemoController {

    @GetMapping("demo1")
    public String demo1(Model model) {
        User user = new User();
        user.setAge(20);
        user.setPassword("123456");
        user.setRoles(Arrays.asList("1", "2", "3", "4", "5", "6"));
        user.setUsername("zhangsan");
        user.setPermissions("user:add;user:update;user:list");
        user.setCreateTime(new Date());
        user.setIdentity("程序员");

        List<UserInfo> users = Arrays.asList(new UserInfo("aa", 11), new UserInfo("bb", 12));
        model.addAttribute("username", "张三");
        model.addAttribute("users", users);

        model.addAttribute("title", "Thymeleaf模板测试");
        model.addAttribute("user", user);
        return "demo1";
    }
}

class UserInfo {
    private String username;
    private Integer age;

    public UserInfo() {
    }

    public UserInfo(String username, Integer age) {
        this.username = username;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}
